window.addEvent('domready', function() {

	// Fluid 16-column Grid
	// Auto-Select Toggle Elements
	
	// Select all boxes with h2 anchor elements and toggle the next div
	$$('.box h2 a').each(function(item) {
		item.setStyle('cursor', 'pointer');
		var elToHide = item.getParent('h2').getNext('div');
		if (elToHide) {
			var mySlide = new Fx.Slide(elToHide);
			item.store('fx', mySlide);

			if (item.hasClass('hidden')){
				mySlide.hide();
			}

			item.addEvent('click', function(e) {
				e.stop();
				var fx = this.retrieve('fx');
				fx.toggle();

				if (item.hasClass('hidden')){
					item.removeClass('hidden').addClass('visible');
				}
				else {
					item.removeClass('visible').addClass('hidden');
				}
			});
		}
	});

	// Kwick Box
	if ($$('.kwick-box').length) {
		var kwicks = $$('.kwick-group');
		kwicks.each(function(kwickgroup, i){			
			var kwickelms = kwickgroup.getElements('.kwicks .kwick');
		  var fx = new Fx.Elements(kwickelms, {wait: false, duration: 200, transition: Fx.Transitions.quadOut});
			
			var context = $$('#kwick-box-'+kwickgroup.get("id"));
			kwickgroup.getElements('.kwicks .kwick').each(function(kwick, i) {
					kwick.addEvent('mouseenter', function(e){
						var obj = {};
						obj[i] = {
							'width': [kwick.getStyle('width').toInt(), 485]
						};
						kwickgroup.getElements('.kwicks .kwick').each(function(other, j){
							if (other != kwick){
								var w = other.getStyle('width').toInt();
								if (w != 125) obj[j] = {'width': [w, 125]};
							}
						});
						fx.start(obj);
					});
			});
			
			kwickgroup.addEvent('mouseleave', function(e){
				var obj = {};
				kwickgroup.getElements('.kwicks .kwick').each(function(kwick, j){
					obj[j] = {'width': [kwick.getStyle('width').toInt(), 215]};
				});
				fx.start(obj);
			});
		
		});
	}
	
	// Accordion Section Menu
	if ($$('.section-menu').length) {
		$$('.section-menu').each(function(section, i) {
				var accordion = new Accordion('a.menuitem', 'ul.submenu', {
					opacity: false,
					onActive: function(toggler, element){
						toggler.addClass('current');
						element.addClass('current');
					},
					onBackground: function(toggler, element){
						toggler.removeClass('current');
						element.removeClass('current');
					}
				}, section);
			});
	}

	// Accordion Content
	if ($$('.accordion').length) {
		$$('.accordion').each(function(accordion, i) {
				var accordion = new Accordion('h3.atStart', 'div.atStart', {
					opacity: false,
					onActive: function(toggler, element){
						toggler.setStyle('font-weight', 'bold');
						toggler.setStyle('background', '#fff');
					},
				 
					onBackground: function(toggler, element){
						toggler.setStyle('font-weight', 'normal');
						toggler.setStyle('background', '#eee');
					}
				}, accordion);
			});
	}

	// Toggle and load AJAX content
	if ($$('.ajax-content').length) {
		// Dynamically loaded AJAX content
		$$('.loading').each(function(loading, i) {
				loading.setStyle('display', 'none'); // hides the loading image
			});
		
		$$('.ajax-content').each(function(ajaxcontent, i) {
				var fx = new Fx.Slide(ajaxcontent).hide(); // creates new Fx.Slide object from ajax-content div, also the hide() function hides the div when the page loads
		
				var elmid = ajaxcontent.get("id");
				var toggle = $$('#toggle-'+elmid);
				var loading = $$('#loading-'+elmid);
				
				toggle.addEvent('click', function(e) { // adds an onClick event to toggle-ajax div
					e = new Event(e); 	
					if (toggle.hasClass('hidden')) { // checks if the content is visible
						ajaxcontent.empty() // empties the ajax-content
						loading.setStyle('display', 'block'); // displays the loading image
		
						var req = new Request.HTML({
							method: 'get',
							url: '/fluid960gs/data/response-'+elmid+'.html',
							update: ajaxcontent,
							onComplete: function(html) { 
								loading.setStyle('display', 'none'); //Hides the loading image
								fx.toggle(); //toggles the ajax-content
							}
						}).send();
					}
					else { 
						fx.toggle(); //in case we dont want to send ajax request, we just slide it out
					}
					if (toggle.hasClass('hidden')){
						toggle.removeClass('hidden').addClass('visible');
					}
					else {
						toggle.removeClass('visible').addClass('hidden');
					}
					e.stop(); //this makes sure that the user wont be sent to given url (or that the page refreshes when using dummy url like "#") if the clicked element was a link 
				});
			});
	}

	// Toggle Grid
	if ($$('.grid').length) {
		$$('.grid').each(function(grid, i) {
				var fx = new Fx.Slide(grid).hide(); //creates new Fx.Slide object from grid div
				var toggle = $$('#toggle-'+grid.get("id"));
				
				toggle.addEvent('click', function(e) { //Adds an onClick event to toggle-grid div
					e = new Event(e);
					fx.toggle(); //toggles the div
					if (toggle.hasClass('hidden')){
						toggle.removeClass('hidden').addClass('visible');
					}
					else {
						toggle.removeClass('visible').addClass('hidden');
					}
					e.stop(); //this makes sure that the user wont be sent to given url (or that the page refreshes when using dummy url like "#") if the clicked element was a link 
				});
			});
	}
});